﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ChongJu.CameraCalibration.Calibration
{
    public class CalibrationSDK
    {
        private const string dllName = "JY_camera_calibration.dll";

        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static IntPtr CreateJY_CC();

        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool GetVersion(IntPtr ptr, StringBuilder version);

        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static void DisposeJY_CC(IntPtr ptr);


        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool Set_ROI(IntPtr prt, int x, int y, int w, int h);


        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool FreeImage(IntPtr image);//*********

        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool Get_image_ROI_RGB_mean(IntPtr ptr, IntPtr image, int x, int y, int w, int h, ref double r_mean, ref double g_mean, ref double b_mean, ref double r_std, ref double g_std, ref double b_std);

        /// <summary>
        /// 设置校准RGB增长数
        /// </summary>
        /// <param name="ptr"></param>
        /// <param name="r_factor1"></param>
        /// <param name="g_factor1"></param>
        /// <param name="b_factor1"></param>
        /// <returns></returns>
        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool Set_adjustment_factor(IntPtr ptr, double r_factor1, double g_factor1, double b_factor1);

        /// <summary>
        /// 设置灰板RGB标准值
        /// </summary>
        /// <param name="ptr"></param>
        /// <param name="r_target1"></param>
        /// <param name="g_target1"></param>
        /// <param name="b_target1"></param>
        /// <returns></returns>
        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool Set_adjustment_target(IntPtr ptr, double r_target1, double g_target1, double b_target1);

        /// <summary>
        /// 粗调校准相机RGB
        /// </summary>
        /// <param name="ptr"></param>
        /// <param name="r_mean"></param>
        /// <param name="g_mean"></param>
        /// <param name="b_mean"></param>
        /// <param name="the"></param>
        /// <param name="r_gain_amount"></param>
        /// <param name="g_gain_amount"></param>
        /// <param name="b_gain_amount"></param>
        /// <param name="flag_Iteration"></param>
        /// <returns></returns>
        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool Get_propose(IntPtr ptr, double r_mean, double g_mean, double b_mean, double the, ref int r_gain_amount, ref int g_gain_amount, ref int b_gain_amount, ref int flag_Iteration);

        /// <summary>
        /// 获取文件夹下所有灰板图像的RGB均值
        /// </summary>
        /// <param name="jy_CC"></param>
        /// <param name="dirPath"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <param name="max_num"></param>
        /// <param name="min_p"></param>
        /// <param name="max_p"></param>
        /// <param name="flag"></param>
        /// <param name="r_mean"></param>
        /// <param name="g_mean"></param>
        /// <param name="b_mean"></param>
        /// <param name="r_std"></param>
        /// <param name="g_std"></param>
        /// <param name="b_std"></param>
        /// <returns></returns>
        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool Get_dir_ROI_RGB_mean(IntPtr jy_CC, IntPtr dirPath, int x, int y, int w, int h, int max_num, double min_p, double max_p, double flag, ref double r_mean, ref double g_mean, ref double b_mean, ref double r_std, ref double g_std, ref double b_std);

        /// <summary>
        /// 粗调校准光源
        /// </summary>
        /// <param name="jy_CC"></param>
        /// <param name="r_mean"></param>
        /// <param name="g_mean"></param>
        /// <param name="b_mean"></param>
        /// <param name="gain_amount"></param>
        /// <param name="flag_Iteration"></param>
        /// <returns></returns>
        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool Get_brightness_propose(IntPtr jy_CC, double r_mean, double g_mean, double b_mean, ref double gain_amount, ref int flag_Iteration);

        /// <summary>
        /// 设置灰板中心点
        /// </summary>
        /// <param name="jy_CC"></param>
        /// <param name="_width"></param>
        /// <param name="_height"></param>
        /// <param name="channel"></param>
        /// <param name="image"></param>
        /// <param name="step"></param>
        /// <param name="center_x"></param>
        /// <param name="center_y"></param>
        /// <param name="newWidth"></param>
        /// <param name="newHeight"></param>
        /// <param name="newStep"></param>
        /// <param name="cir_size"></param>
        /// <returns></returns>
        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static IntPtr Add_center_mark(IntPtr jy_CC, int _width, int _height, int channel, IntPtr image, int step, int center_x, int center_y, ref int newWidth, ref int newHeight, ref int newStep, int cir_size);

        [DllImport(dllName, CallingConvention = CallingConvention.Cdecl)]
        public extern static bool SaveStandHuibanImg(IntPtr jy_CC, IntPtr dirPath, int x, int y, int w, int h, int max_num, double min_p, double flag);

    }
}